#include "./player.h"


Player::Player(QWidget *w) :
    m_win(w)
{

}

Player::~Player() {
    if (m_task != nullptr) {
        qDebug() << "delete player";
        m_task->exit();
        m_task = nullptr;
        m_play_task.join();
    }
}

void Player::play(const QString &file_path) {
    if (m_task != nullptr) {
        m_task->exit();
        m_task = nullptr;
        m_play_task.join();
    }

    try {
        m_task = std::make_unique<PlayTask>( file_path, m_win );
        m_play_task = std::thread([this](){

            try {
                m_task->play();
            } catch (const std::exception &e) {
                qDebug() << e.what();
            }

            qDebug() << "play finish";
        });
    } catch (const std::exception &e) {
        qDebug() << e.what();
    }


}
